﻿#include "EquipUI.h"
#include "Map.h"
#include "Item.h"

EquipUI::EquipUI()
{
	initWithFile("ui/PlayerEquip/Charaacter_Equip_Main.png");
	autorelease();
	EquipSlots.clear();
	InitSlot();
}

EquipUI::~EquipUI()
{

}

void EquipUI::LoadSlotFromDB()
{
	std::string msg = "SELECT guid,Owner,Entry FROM inventory_instance WHERE Owner = ";
	char msga[50];
	snprintf(msga, 50, "%d AND slot >= 300 AND slot <= 326", Map_Scene::GetInstance()->GetLoadedPlayer()->GetGuid());
	msg += msga;
	Result::Qurey result = Database::GetInstance()->GetResult(CharacterDatabase, msg.c_str());
	if (!result.empty())
	{
		for (Result::Qurey::iterator itr = result.begin(); itr != result.end(); itr++)
		{
			Item* TempItem = new Item(atoi(itr->second.at(2).c_str()), Map_Scene::GetInstance()->GetLoadedPlayer(), atoi(itr->second.at(0).c_str()));
		}
	}
}

Slot* EquipUI::GetSlot(int Tag)
{
	if (EquipSlots.find(Tag) != EquipSlots.end())
		return EquipSlots[Tag];
	return nullptr;
}

int EquipUI::GetTouchedSlot(Vec2 Ptouch)
{
	Vec2 touchLocation = Ptouch; // Get the touch position
	for (std::map<int, Slot*>::iterator itr = EquipSlots.begin(); itr != EquipSlots.end(); itr++)
	{
		if (itr == EquipSlots.end())
			break;
		if (Slot* pSlot = itr->second)
		{
			Vec2 check = pSlot->getParent()->convertToNodeSpace(touchLocation);
			Rect bBox = pSlot->getBoundingBox();
			if (bBox.containsPoint(check))
				return itr->first;
		}
	}
	return PLAYER_EQUIP_UI_TAG;
}

void EquipUI::InitSlot()
{
	//14-50   88 91
	int k = RING_ZERO;
	for (int i = PLAYER_EQUIP_SLOT_START; i != PLAYER_EQUIP_SLOT_END + 1; i++, k++)
	{
		Slot* TempSlot = new Slot(0, true, k);
		TempSlot->setTag(i);
		float SinglePlus = 3 + TempSlot->getBoundingBox().size.height;
		if (k <= RING_FIVE)
			TempSlot->setPosition(14.2f + TempSlot->getBoundingBox().size.width / 2, getBoundingBox().size.height - 50 - (TempSlot->getBoundingBox().size.height / 2) - (SinglePlus) * k);
		else if (k <= BELT)
			TempSlot->setPosition(getChildByTag(RING_ONE + PLAYER_EQUIP_SLOT_START)->getPositionX() + getChildByTag(RING_ONE + PLAYER_EQUIP_SLOT_START)->getBoundingBox().size.width + 3, getChildByTag(RING_ONE + PLAYER_EQUIP_SLOT_START)->getPositionY() - (SinglePlus * (k - 6)));
		else if (k <= SHOES)
			TempSlot->setPosition(getChildByTag(NECKLACE_ZERO + PLAYER_EQUIP_SLOT_START)->getPositionX() + getChildByTag(NECKLACE_ZERO + PLAYER_EQUIP_SLOT_START)->getBoundingBox().size.width + 3, getChildByTag(RING_ZERO + PLAYER_EQUIP_SLOT_START)->getPositionY() - (SinglePlus * (k - 10)));
		else if (k <= ROBOT)
			TempSlot->setPosition(getChildByTag(HEAD + PLAYER_EQUIP_SLOT_START)->getPositionX() + getChildByTag(HEAD + PLAYER_EQUIP_SLOT_START)->getBoundingBox().size.width + 3, getChildByTag(RING_ONE + PLAYER_EQUIP_SLOT_START)->getPositionY() - (SinglePlus * (k - 16)));
		else if (k <= ROBOT_HEART)
			TempSlot->setPosition(getChildByTag(ROBOT + PLAYER_EQUIP_SLOT_START)->getPositionX() + getChildByTag(ROBOT + PLAYER_EQUIP_SLOT_START)->getBoundingBox().size.width + 3, getChildByTag(RING_ZERO + PLAYER_EQUIP_SLOT_START)->getPositionY() - (SinglePlus * (k - 21)));
		
		EquipSlots[TempSlot->getTag()] = TempSlot;
		addChild(TempSlot);
	}
}